package com.etc.emsys.controller.emp;

import com.etc.emsys.controller.BaseMenuController;
import com.etc.emsys.dao.emp.DeptDao;
import com.etc.emsys.dao.emp.EmpDao;
import com.etc.emsys.domain.DeptPojo;
import com.etc.emsys.domain.EmpPojo;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;

import java.util.List;
import java.util.Optional;

public class EmpController extends BaseMenuController {



    @FXML
    TextField enametextfiled;
    @FXML
    ComboBox deptcombox;

    @FXML
    TableView<EmpPojo> table;

    @FXML
    Button search;
    @FXML
    Button reset;
    @FXML
    TableColumn<EmpPojo, Integer> colnum;

    @FXML
    TableColumn<EmpPojo, Integer> colempno;
    @FXML
    TableColumn<EmpPojo, String> colename;
    @FXML
    TableColumn<EmpPojo, String> coldname;
    @FXML
    TableColumn<EmpPojo, String> colhandle;
    // 初始化方法
    public void init() {
//        List<DeptPojo> list = new ArrayList<DeptPojo>();
////        list.add(new DeptPojo(0, "请选择部门"));
//        list.add(new DeptPojo(1, "a1"));
//        list.add(new DeptPojo(2, "a2"));
//        deptcombox.setItems(FXCollections.observableList(list));


        DeptDao dao = new DeptDao();
        List<DeptPojo> list = dao.selectAllDept();
        deptcombox.setItems(FXCollections.observableList(list));
    }

    /**
     * 重置按钮
     */
    public void reset() {
        enametextfiled.setText("");
        deptcombox.setValue("");
//        deptcombox.getScene().getRoot().setPop
    }

    /**
     * 检索按钮
     */
    public void search() {
//        List<EmpPropertyPojo> elist = new ArrayList<>();
//        EmpPropertyPojo pojo = new EmpPropertyPojo();
//        pojo.setEmpno(1);
//        pojo.setEname("aaaa");
//        pojo.setDname("bbb");
//        elist.add(pojo);

        EmpDao empdao = new EmpDao();
        String ename = enametextfiled.getText();
        DeptPojo dvalue = new DeptPojo();
        if (deptcombox.getValue() instanceof DeptPojo) {
            dvalue = (DeptPojo) deptcombox.getValue();
        }
        List<EmpPojo> elist = empdao.searchByCondition(ename, dvalue.getDeptno());
        ObservableList<EmpPojo> olist = FXCollections.observableList(elist);
        table.setItems(olist);

        colempno.setCellValueFactory(new PropertyValueFactory<EmpPojo, Integer>("empno"));
        colename.setCellValueFactory(new PropertyValueFactory<EmpPojo, String>("ename"));
        coldname.setCellValueFactory(new PropertyValueFactory<EmpPojo, String>("dname"));

        // 添加行号
        colnum.setCellValueFactory((colData) -> {
            return new ReadOnlyObjectWrapper<Integer>(
                    table.getItems().indexOf(colData.getValue()) + 1);
        });
        colhandle.setCellFactory((col) -> {
            TableCell<EmpPojo, String> cell = new TableCell<EmpPojo, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setText(null);
                    this.setGraphic(null);
                    if (!empty) {
                        Button updBtn = new Button("更新");
                        Button delBtn = new Button("删除");
                        HBox hbox = new HBox();

                        hbox.getChildren().addAll(updBtn, delBtn);
                        hbox.setSpacing(10);
                        this.setGraphic(hbox);
                        delBtn.setOnMouseClicked((me) -> {
                            EmpPojo emp = this.getTableView().getItems().get(this.getIndex());
                            Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Are you sure delete" + emp.getEname(),
                                    new ButtonType("取消", ButtonBar.ButtonData.NO), new ButtonType("确定", ButtonBar.ButtonData.YES));
                            Optional optional = alert.showAndWait();
                            if(((ButtonType)optional.get()).getButtonData().equals(ButtonBar.ButtonData.YES)) {

                                System.out.println("删除 " + emp.getEname() + " 的记录");
                                EmpDao dao = new EmpDao();
                                dao.deleteByEmpno(emp.getEmpno());
                                olist.remove(this.getIndex());
                                Alert alert1 = new Alert(Alert.AlertType.INFORMATION, "删除 " + emp.getEname() + " 的记录");
                                alert1.showAndWait();

                            } else {

                            }
                        });

                        updBtn.setOnAction((actionEvent) -> {
                            EmpPojo emp = this.getTableView().getItems().get(this.getIndex());
                            EmpController.this.transform(this.getScene(), "../../view/emp/empUpd.fxml", "更新页面", emp);

                        });
                    }

                }
            };


            return cell;
        });
    }

}

